/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.home.table.editor;

import com.home.common.ApplicationCommon;
import com.home.common.Constants;
import com.home.item.SchemeComboItem;
import com.home.mapping.Attribute;
import com.home.model.table.ScorecardDetailObjTable;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author vi
 */
public class ScorecardDetailsTableModel extends AbstractTableModel {
    String[] colNames = {"Group Attribute", "Attributes", "Evaluate Score", "Calculated Score"};
    List<ScorecardDetailObjTable> listData;
    

    public ScorecardDetailsTableModel() {
    }

    public ScorecardDetailsTableModel(List<ScorecardDetailObjTable> listData) {
        this.listData = listData;
    }

    @Override
    public int getRowCount() {
        return listData.size();
    }

    @Override
    public String getColumnName(int column) {
        return colNames[column];
    }

    @Override
    public int getColumnCount() {
        return colNames.length;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {        
        if (aValue != null) {
            if (columnIndex == Constants.TABLE_SCORECARD_DETAILS_COLUMN_EVALUATE_SCORE) {
                SchemeComboItem schemeItem = (SchemeComboItem)aValue;
                Attribute attr = listData.get(rowIndex).getAttribute();
                listData.get(rowIndex).setScore(schemeItem.getValue());   
                
                listData.get(rowIndex).setCalculatedScore(ApplicationCommon.calculateResultOfAttributeWhenSelect(attr, schemeItem.getValue()));
                fireTableCellUpdated(rowIndex, columnIndex);

            }     
        }
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        String returnValue = "";
        switch (columnIndex) {
            case 0:
                return listData.get(rowIndex).getAttributeGroup();
            case 1:
                return listData.get(rowIndex).getAttribute();
            case 2:
                return listData.get(rowIndex).getScore();
            case 3:
                return listData.get(rowIndex).getCalculatedScore();
            
        }
        return returnValue;

    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        
        return columnIndex == Constants.TABLE_SCORECARD_DETAILS_COLUMN_EVALUATE_SCORE;
    }

}
